<template>
  <div>
    <button class="update-button" style="width:70px; height:40px;" @click="clickUpd()">编辑</button>

    <input id="updateBtn" type="hidden" data-toggle="modal" data-target=".update-Dialog">

    <div
      class="modal fade update-Dialog"
      style="color: black;"
      tabindex="-1"
      role="dialog"
      aria-hidden="true"
    >
      <div class="modal-dialog modal-lg">
        <div class="modal-content" :style="backgroundColor">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">
              <span aria-hidden="true">×</span>
            </button>
            <div>请修改以下信息</div>
          </div>
          <!-- 表单内容 start ================================================ -->
          <div class="x_content">
            <form class="form-horizontal form-label-left" id="updateForm">
              <br>

              <div class="item form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">账号</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                  <!-- height:0px/margin-bottom:0px;：不给实际高度,只是除去原有样式 -->
                  <input
                    name="account"
                    class="form-control col-md-7 col-xs-12"
                    :style="input"
                    type="text"
                  >
                </div>
              </div>

              <div class="item form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">性别</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                  <div id="gender" class="btn-group" data-toggle="buttons">
                    <label
                      class="btn btn-default"
                      data-toggle-class="btn-primary"
                      data-toggle-passive-class="btn-default"
                    >
                      <input type="radio" name="gender" value="1"> &nbsp; 男 &nbsp;
                    </label>
                    <label
                      class="btn btn-default"
                      data-toggle-class="btn-primary"
                      data-toggle-passive-class="btn-default"
                    >
                      <input type="radio" name="gender" value="0"> &nbsp; 女 &nbsp;
                    </label>
                  </div>
                </div>
              </div>

              <div class="item form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">昵称</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                  <input
                    name="nickname"
                    :style="input"
                    class="form-control col-md-7 col-xs-12"
                    type="text"
                  >
                </div>
              </div>

              <div class="item form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">地区</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                  <input
                    name="region"
                    :style="input"
                    style="margin-bottom:15px;"
                    type="text"
                    class="form-control col-md-7 col-xs-12"
                  >
                </div>
              </div>
            </form>
          </div>
          <!-- 表单内容 end ================================================ -->
          <div class="modal-footer">
            <button
              id="send"
              type="submit"
              @click="updateUserFun"
              class="btn btn-success"
              data-dismiss="modal"
            >提交</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 巴士：用于兄弟组件间传值
import bus from "../../../../../static/base/eventBus.js";

var userIds = "0"; //　初始化已选中id集合。
var updateId;

export default {
  name: "updateBtn",
  data() {
    return {
      // 表单设置
      input: {
        fontSize: "21px",
        height: "0px",
        marginBottom: "0px"
      },
      backgroundColor: {
        background: "#f5fffa"
      }
    };
  },
  methods: {
    // 执行编辑
    updateUserFun() {
      this.$axios({
        method: "post",
        url: this.GLOBAL.BASE_URL + "/user/updateUser?id=" + this.updateId,
        data: decodeURI($("#updateForm").serialize())
      }).then(res => {
        bus.$emit("refresh", refresh); // 重置复选框
        alert("编辑成功！");
      });

      var refresh = 1;
      bus.$emit("refresh", refresh); // 重置复选框 或 编辑成功--刷新表单
    },
    // 填充预编辑数据
    clickUpd: function() {
      if (0 == this.userIds || this.userIds.length != 1) {
        alert("请选择一行数据！");
        this.userIds = "0"; // 重置id集合
        return false;
      }

      var params = new URLSearchParams(); // 请求接口封装参数
      this.updateId = this.userIds;
      params.append("id", this.userIds);
      this.$axios({
        method: "post",
        url: this.GLOBAL.BASE_URL + "/user/getUser",
        data: decodeURI(params) // 解决中文乱码
      }).then(res => {
        // 数据格式：[1, "admin", "全局管理员", "小熊", "1", 1546425184000, "成都"]
        $('input[name="account"]').val(res.data[1][1]);
        $('input[name="nickname"]').val(res.data[1][3]);
        $('input[name="region"]').val(res.data[1][6]);
        // TODO 单选框设置其中一个选中
        // $("input[name='gender'][value='0']").attr("checked",true);
        // $("input[name='gender'][value='" + res.data[0][4] + "']").prop( "checked",true);
      });

      var refresh = 1;
      bus.$emit("refresh", refresh); // 重置复选框
      $("#updateBtn").click(); // 弹出编辑框
    }
  },
  created() {
    // 监听刷新 -- 重新初始化已选中id集合
    bus.$on("refresh", refresh => {
      if (refresh != null) {
        this.userIds = "0";
      }
    });

    this.userIds = "0";
    // 监听兄弟组件oneTable的传参：要编辑的数据
    bus.$on("userIds", userIds => {
      this.userIds = userIds;
    });

    // 初始化
    var animateButton = function(e) {
      e.preventDefault;
      e.target.classList.remove("animate");
      e.target.classList.add("animate");
      setTimeout(function() {
        e.target.classList.remove("animate");
      }, 700);
    };
    var classname = document.getElementsByClassName("update-button");
    for (var i = 0; i < classname.length; i++) {
      classname[i].addEventListener("click", animateButton, false);
    }
  }
};
</script>

<style>
.update-button {
  font-family: "Helvetica", "Arial", sans-serif;
  display: inline-block;
  font-size: 14px;
  margin-top: 10px;
  margin-bottom: 10px;
  -webkit-appearance: none;
  appearance: none;
  background-color: #f1f0abd8;
  color: #fff;
  /* 圆角的程度 */
  border-radius: 4px;
  border: none;
  cursor: pointer;
  position: relative;
  /* 过渡 */
  transition: transform ease-in 0.1s, box-shadow ease-in 0.25s;
  box-shadow: 0 2px 25px rgba(233, 163, 223, 0.6);
}
.update-button:focus {
  outline: 0;
}
.update-button:before,
.update-button:after {
  position: absolute;
  content: "";
  display: block;
  width: 70%;
  height: 50%;
  left: -20%;
  z-index: -1000;
  transition: all ease-in-out 0.5s;
  background-repeat: no-repeat;
}
.update-button:before {
  display: none;
  top: -75%;
  background-image: radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, transparent 20%, #f1f0abd8 20%, transparent 30%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, transparent 10%, #f1f0abd8 15%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%);
  background-size: 10% 10%, 20% 20%, 15% 15%, 20% 20%, 18% 18%, 10% 10%, 15% 15%,
    10% 10%, 18% 18%;
}
.update-button:after {
  display: none;
  bottom: -75%;
  background-image: radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, transparent 10%, #f1f0abd8 15%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%),
    radial-gradient(circle, #f1f0abd8 20%, transparent 20%);
  background-size: 15% 15%, 20% 20%, 18% 18%, 20% 20%, 15% 15%, 10% 10%, 20% 20%;
}
.update-button:active {
  transform: scale(0.9);
  background-color: #f1f0abd8;
  box-shadow: 0 2px 25px rgba(255, 0, 130, 0.2);
}
.update-button.animate:before {
  display: block;
  animation: topBubbles ease-in-out 0.75s forwards;
}
.update-button.animate:after {
  display: block;
  animation: bottomBubbles ease-in-out 0.75s forwards;
}

@keyframes topBubbles {
  0% {
    background-position: 5% 90%, 10% 90%, 10% 90%, 15% 90%, 25% 90%, 25% 90%,
      40% 90%, 55% 90%, 70% 90%;
  }
  50% {
    background-position: 0% 80%, 0% 20%, 10% 40%, 20% 0%, 30% 30%, 22% 50%,
      50% 50%, 65% 20%, 90% 30%;
  }
  100% {
    background-position: 0% 70%, 0% 10%, 10% 30%, 20% -10%, 30% 20%, 22% 40%,
      50% 40%, 65% 10%, 90% 20%;
    background-size: 0% 0%, 0% 0%, 0% 0%, 0% 0%, 0% 0%, 0% 0%;
  }
}
@keyframes bottomBubbles {
  0% {
    background-position: 10% -10%, 30% 10%, 55% -10%, 70% -10%, 85% -10%,
      70% -10%, 70% 0%;
  }
  50% {
    background-position: 0% 80%, 20% 80%, 45% 60%, 60% 100%, 75% 70%, 95% 60%,
      105% 0%;
  }
  100% {
    background-position: 0% 90%, 20% 90%, 45% 70%, 60% 110%, 75% 80%, 95% 70%,
      110% 10%;
    background-size: 0% 0%, 0% 0%, 0% 0%, 0% 0%, 0% 0%, 0% 0%;
  }
}
</style>